package BanyuanClub.Study.Day0308.多线程.习题二;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TextCallable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //平均数
        int avg = 100/3;
        //创建一个线程数组
        FutureTask[] ftarr = new FutureTask[3];
        //循环创建子线程
        for (int i = 0; i < 3; i++) {
            //将i的值赋给一个变量，方便之后调用
            int index = i;
            FutureTask<Integer> ft = new FutureTask<>(()->{
                int sum = 0;
                int stark = index * avg + 1;
                int end = index < 2 ? stark + avg - 1 : 100;
                for (int j = stark; j <= end; j++) {
                    sum += j;
                }
                return sum;
            });
            ftarr[i] = ft;
        }

        for (FutureTask<Integer> ft : ftarr
             ) {
            new Thread(ft).start();
        }
        int all = 0;
        for (FutureTask<Integer> ft : ftarr
             ) {
            all += ft.get();
        }

        System.out.println("总和为：" + all);
    }
}
